"""
json:作为一种中转的数据格式,提供给不同的计算机语言进行交互
json:的本质是有特殊格式的字符串
json的数据格式:就相当于python 字典 和 列表(内嵌字典的列表
"""
import json
"""
演示json数据和python字典的相互转换
"""
#1.准备列表,列表中每一个元素都是字典,将其转换为JSON
data = [{"name": "王大锤", "age": 23},{"name": "找大三", "age": 15},{"name": "高启强", "age": 56}]

# json_str = json.dumps(data)
# print(json_str)  >>>[{"name": "\u738b\u5927\u9524", "age": 23}, {"name": "\u627e\u5927\u4e09", "age": 15}, {"name": "\u9ad8\u542f\u5f3a", "age": 56}]
#不转ascii显示
json_str = json.dumps(data, ensure_ascii= False)

print(type(json_str))    #>>>>[{"name": "王大锤", "age": 23}, {"name": "找大三", "age": 15}, {"name": "高启强", "age": 56}]
print(json_str)

#2.准备字典,将字典转化为JSON
d = {"name": "周杰伦", "addr": "台北"}
json_str = json.dumps(d,ensure_ascii=False)
print(type(json_str))
print(json_str)

# 3将JSON字符串转化为Python数据类型dict
s = '{"name": "周杰伦", "addr": "台北"}'
d = json.loads(s)
print(type(d))
print(d)

# 3将JSON字符串转化为Python数据类型list
s = '[{"name": "王大锤", "age": 23},{"name": "找大三", "age": 15},{"name": "高启强", "age": 56}]'
d = json.loads(s)
print(type(d))
print(d)